package jinyilw.swing.visuals;

import jinyilw.swing.visuals.label.SwitchLabel;

import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

public class SwitchGroup extends JPanel // 开关组面板
{
	private static final long serialVersionUID = 7014055324286989412L;
	ArrayList<SwitchLabel> list = new ArrayList<>();
	Listener listener = new Listener();
	public SwitchGroup()
	{
		setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
		setOpaque(false);
	}
	public void switchTo(SwitchLabel sl)
	{
		if (!sl.isOpened())
		{
			for (SwitchLabel s : list)
				if (s.isOpened() && s != sl)
					s.close();
			sl.open();
		}
	}
	public void addSwitchLabel(SwitchLabel sl)
	{
		add(sl);
		list.add(sl);
		sl.addMouseListener(listener);
	}
	private class Listener extends MouseAdapter
	{
		@Override
		public void mouseClicked(MouseEvent e)
		{
			SwitchLabel sl = (SwitchLabel) e.getSource();
			switchTo(sl);
		}
	}
	@Override
	public void addMouseListener(MouseListener listener)
	{
		// super.addMouseListener(listener);
		for (SwitchLabel s : list)
			s.addMouseListener(listener);
	}
}
